package com.onesports.intelligent.k12.polarlight.controller.operation.account;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.onesports.framework.kit.common.result.PageResult;
import com.onesports.framework.kit.common.valid.Json;
import com.onesports.intelligent.k12.polarlight.common.utils.ResultUtil;
import com.onesports.intelligent.k12.polarlight.constant.ApiPathConstants;
import com.onesports.intelligent.k12.polarlight.constant.BizConstants;
import com.onesports.intelligent.k12.polarlight.domain.dto.common.EditStatusDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.common.IdDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.sys.RunAccountDTO;
import com.onesports.intelligent.k12.polarlight.domain.entity.sys.Role;
import com.onesports.intelligent.k12.polarlight.domain.vo.sys.RunAccountVO;
import com.onesports.intelligent.k12.polarlight.service.sys.AccountService;
import com.onesports.intelligent.k12.polarlight.service.sys.RoleService;
import com.onesports.intelligent.k12.polarlight.service.sys.RunAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "运营端-系统管理-用户管理")
@RestController
@RequiredArgsConstructor
@RequestMapping(ApiPathConstants.OPERATION_ACCOUNT)
public class AccountController {

    private final RunAccountService runAccountService;
    private final AccountService accountService;
    private final RoleService roleService;

    @ApiOperation("添加")
    @PostMapping("/add")
    public ResponseEntity<String> add(@Json @RequestBody RunAccountDTO dto){
        this.runAccountService.add(dto, BizConstants.OPERATION_ORGANIZATION_ID);
        return ResultUtil.success("添加成功!");
    }

    @ApiOperation("编辑")
    @PostMapping("/edit")
    public ResponseEntity<String> edit(@Json @RequestBody RunAccountDTO dto){
        dto.setOrganizationId(BizConstants.OPERATION_ORGANIZATION_ID);
        this.runAccountService.edit(dto);
        return ResultUtil.success("操作成功!");
    }

    @GetMapping("/page")
    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", required = true, paramType = "query", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "row", value = "条数", required = true, paramType = "query", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "phone", value = "手机号", paramType = "query", dataTypeClass = String.class),
            @ApiImplicitParam(name = "username", value = "姓名", paramType = "query", dataTypeClass = String.class),
            @ApiImplicitParam(name = "status", value = "状态true：启用 false:停用", paramType = "query", dataTypeClass = Boolean.class),
            @ApiImplicitParam(name = "gender", value = "性别：1 男 2:女", paramType = "query", dataTypeClass = String.class),
    })
    public ResponseEntity<IPage<RunAccountVO>> page(Long page, Long row, String phone, String username, Boolean status, String gender) {
        return ResultUtil.success(this.runAccountService.page(new Page<>(page, row),phone,username,status,gender, BizConstants.OPERATION_ORGANIZATION_ID));
    }

    @PostMapping("/reset-pwd")
    @ApiOperation("重置密码")
    public ResponseEntity<String> resetPwd(@Json @RequestBody IdDTO dto) {
        this.accountService.resetPwd(dto);
        return ResultUtil.success("操作成功!");
    }


    @PostMapping("/update-status")
    @ApiOperation("启用/停用")
    public ResponseEntity<String> updateStatus(@Json @RequestBody EditStatusDTO dto) {
        this.accountService.updateStatus(dto);
        return ResultUtil.success("操作成功");
    }


    @GetMapping("/all-role")
    @ApiOperation("添加页获取所有角色")
    public ResponseEntity<List<Role>> allRole() {
        return ResultUtil.success(this.roleService.allRole(BizConstants.OPERATION_ORGANIZATION_ID));
    }
}
